﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using KAImage.Config;

namespace KAImage.AlgorAction
{
    public class CustomActions
    {
        private AlgorithmApplyingOption _option = KAConfigs.AlgorApplying;

        private Bitmap _source = null;

        private Bitmap _destImage = null;
        public Bitmap DestImage
        {
            get { return _destImage; }
        }

        private List<IAlgorAction> actionList = new List<IAlgorAction>();

        public WorkImageUpdatedEventHandler WorkImageUpdated = null;
        protected void OnWorkImageUpdated(Bitmap image)
        {
            if (WorkImageUpdated != null)
            {
                WorkImageUpdated(this, new WorkImageUpdatedEventArgs(image));
            }
        }

        public CustomActions(Bitmap source)
        {
            _source = source.Clone() as Bitmap;
            CreatActions();        
        }

        private void CreatActions()
        {          
            if(_option.AdjustEnabled)
            {
                if(_option.BlurSharpen)
                {
                    actionList.Add(new BlurSharpenAction());
                }
                if(_option.Curve)
                {
                    actionList.Add(new CurveAction());
                }
                if(_option.HSL)
                {
                    actionList.Add(new HSLAction());
                }
                if(_option.Levels)
                {
                    actionList.Add(new LevelsAction());
                }
            }           
            actionList.Sort(delegate(IAlgorAction t1, IAlgorAction t2)
            {
                return t1.CallIndex.CompareTo(t2.CallIndex);
            });
        }

        /// <summary>
        /// 执行算法调节
        /// </summary>
        public void Execute()
        {
            foreach(IAlgorAction a in actionList)
            {
                a.DoAction(_source);
            }
            if (_option.Megre)
            {
                MegreAction action = new MegreAction();
                _destImage = action.DoAction(_source);
            }
            else
            {
                _destImage = _source;
            }
            OnWorkImageUpdated(_destImage);
        }
    }
}
